﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Grishko.SerilizeTools;

namespace Grishko.DataTier.SqlSentence
{
    /// <summary>
    /// Основа для Фильтрации списокв - Само предложения поиска + условия сортировки + набор обязательных параметров применяемых к запросу    
    /// </summary>
    
    public enum SortDirection
    {
        // Summary:
        //     Sort from smallest to largest. For example, from A to Z.
        Ascending = 0,
        //
        // Summary:
        //     Sort from largest to smallest. For example, from Z to A.
        Descending = 1,
    }

    [IsNotSrchStorageLinksObject]  
    public class ListWhere : ObjectWhereSentence   
    {
        public ListWhere(){}
        public ListWhere.TSortCondition SortCondition { get; set; }

        /// <summary>
        /// Вычисляет Tagname - объекта указанного 
        /// </summary>
        public string ObjectTagName { get 
        {
            if (null == EntityTypeInfo.EntityType)
                return string.Empty;

            object[] arr = EntityTypeInfo.EntityType.GetCustomAttributes(typeof(TagNameInfo), true);
            TagNameInfo _atr = (TagNameInfo)arr[0];
            return _atr.TagName;
        } 
        }
    
        /// <summary>
        /// как пример EmployeeList.MyListWhere  - вернет EmployeeList
        /// </summary>
        public virtual ObjectWhere_EntityList GetListInstance()    
        {
            return null; 
        }

        public class TSortCondition : EntitySingle, IComparer
        {
            public TSortCondition() { }

            public Func<ListWhere.TSortCondition, object, object, int> fnOrderBy { get; set; }
            public System.Web.UI.WebControls.SortDirection SortDirection { get; set; }
            public string SortField { get; set; }

            public int Compare(object x, object y) { return 1; }
        }

        /// <summary>
        /// Senteces- дерево 
        /// MySentecesList- развернутый в список дерево 1 элемент - корень
        /// </summary>
        /// <returns>
        public List<WhereSentenceWord> ConditionList
        {
            get
            {
                List<WhereSentenceWord> result = new List<WhereSentenceWord>();
                if (null == Sentence)
                    return result;

                WhereSentenceWord i = Sentence;
                i.ChildNodes.Reset();
                result.Add(i);
                foreach (WhereSentenceWord item in i.ChildNodes)
                {
                    result.Add(item);
                }
                return result;
            }
        }

        /// <summary>
        /// Обязательные (скрытые) условия 
        /// </summary>
        public List<WhereSentenceWord> MandatoryConditionList
        {
            get
            {
                return this.MandatoryConditions;
            }
        }
        
        /// <summary>
        /// MandatoryConditionList добавить условие в MandatoryConditionList
        /// добавляет как and параметр=значение 
        /// </summary>
        public void AddMandatoryCondition_Eq(String FieldId,string _Value)
        {
            WhereSentenceWord w = new WhereSentenceWord() { WordType = WhereSentenceWord.WhereSentenceWordTypes.AND };
            WhereSentenceWord.SentenceWordField F1 = GetSentenceDictionaryField(FieldId,WhereSentenceWord.SentenceWordOperatorTypes.EQUALLY);
            F1.Value = new WhereSentenceWord.WordFieldValue() { Value = _Value };
            w.WordFields.Add(F1);
            MandatoryConditionList.Add(w);
        }

        public void AddMandatoryCondition_Eq(String FieldId,  TreeValueProperty  _Value)
        {            
            WhereSentenceWord w = new WhereSentenceWord() { WordType = WhereSentenceWord.WhereSentenceWordTypes.AND };
            WhereSentenceWord.SentenceWordField F1 = GetSentenceDictionaryField(FieldId, WhereSentenceWord.SentenceWordOperatorTypes.EQUALLY);
            F1.Value = new WhereSentenceWord.WordFieldValue_TreeValue() { Value = _Value };
            w.WordFields.Add(F1);
            MandatoryConditionList.Add(w);
        }

        /// <summary>
        /// Добавляет к списку обязательных ограничений ограничение на значение по like
        /// </summary>
        /// <param name="_Value"></param>
        public void AddMandatoryCondition_Like(string _Value)
        {
            WhereSentenceWord w = new WhereSentenceWord() { WordType = WhereSentenceWord.WhereSentenceWordTypes.AND };
            //WhereSentenceWord.SentenceWordField F1 = GetSentenceDictionaryField(FieldId, WhereSentenceWord.SentenceWordOperatorTypes.EQUALLY);
            WhereSentenceWord.SentenceWordField F1 = new WhereSentenceWord.SentenceWordField();
            
            F1.OperationType = WhereSentenceWord.SentenceWordOperatorTypes.LIKE;
            F1.PropertyType = SrchItemInfo.PropertyControlType.OnlyValueSrch;
            F1.Value = new WhereSentenceWord.WordFieldValue() { Value = _Value };
            w.WordFields.Add(F1);
            MandatoryConditionList.Add(w);
        }
                
        public void AddCondition(WhereSentenceWord obj)
        {
            Sentence.ChildNodes.Add(obj); 
        }
        
        public void RemoveCondition(WhereSentenceWord  obj)
        {
            Sentence.ChildNodes .Remove (obj);
            
        }
        
        
    }
   

}
